package cn.net.shuxing.yifu.common.back.controller;

import cn.net.shuxing.yifu.common.bean.SysUser;
import cn.net.shuxing.yifu.common.bean.UserBankCard;
import cn.net.shuxing.yifu.common.dto.BaseDto;
import cn.net.shuxing.yifu.common.service.CityAgentCommissionService;
import cn.net.shuxing.yifu.common.service.SysUserService;
import cn.net.shuxing.yifu.common.service.UserBankCardService;
import cn.net.shuxing.yifu.mysiteforme.admin.base.MySysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 兼职股东管理和城市代理商Controller
 */
@RestController
@RequestMapping("/back/shareholder")
public class ShareholderController {


    @Autowired
    SysUserService sysUserService;

    @Autowired
    CityAgentCommissionService cityAgentCommissionService;

    @Autowired
    UserBankCardService userBankCardService;

    /**
     * 查询所有兼职股东列表
     * @return
     */
    @RequestMapping("/list")
    public BaseDto List(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                        @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize,
                        String nickName, String tel, String idCardNum,String startTime,String endTime) {
        Integer startPageNo = null;
        if (null != pageSize && null != pageNum) {
            startPageNo = (pageNum - 1) * pageSize;
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData(sysUserService.selectSysUserList(4,startPageNo,pageSize,nickName,tel,idCardNum,startTime,endTime));
        return baseDto;
    }

    /**
     * 新增兼职股东  平台输入股东姓名、手机号、开户银行、开户支行、银行卡号、登录密码  进行股东创建
     */
    @RequestMapping(value = "/partner/add", method = RequestMethod.POST)
    public BaseDto add(String idCardNum,String nickName,Integer cardTypeId,
                       String tel, String openBank,String openBankBranch,
                       String bankCardNum,String password){
        return sysUserService.add(idCardNum,nickName,cardTypeId, tel, openBank,openBankBranch,bankCardNum,password);
    }



    /**
     * 获取兼职股东信息
     * @param userId
     * @return
     */
    @RequestMapping("/partner/edit")
    public BaseDto update(String userId){
        return sysUserService.selectById(Long.valueOf(userId));
    }

    /**
     * 获取兼职股东可提现佣金
     * @return
     */
    @RequestMapping("/comm/withdraw")
    public BaseDto commWithdraw(){
        Long userId = MySysUser.id();
        return sysUserService.selectById(Long.valueOf(userId));
    }

    /**
     * 兼职股东-财务管理-佣金提现
     * @return
     */
    @RequestMapping(value = "/withdraw", method = RequestMethod.POST)
    public BaseDto withdraw(BigDecimal withdrawMoney){
        Long userId = MySysUser.id();
        return cityAgentCommissionService.updateCommission(userId,withdrawMoney);
    }

    /**
     * 编辑
     * @param nickName
     * @param tel
     * @param userId
     * @return
     */
    @RequestMapping(value = "/partner/update" ,method = RequestMethod.POST)
    public BaseDto update(String nickName,String tel,Integer userId){
        return sysUserService.update(userId, nickName, tel);
    }

    /**
     * 冻结和解冻
     * @param type  1-冻结  2-解冻
     * @param userId
     * @return
     */
    @RequestMapping("/freeze")
    public BaseDto freeze(Integer type,Integer userId){
        return sysUserService.freeze(type,userId);
    }




//    兼职股东后台
    /**
     * 获取兼职股东的详细信息
     */
    @RequestMapping("/detail")
    public BaseDto detail(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                          @RequestParam(defaultValue = "1",value = "pageSize")Integer pageSize){
        Long userId = MySysUser.id();
        return sysUserService.detail(userId);
    }

    /**
     * 获取兼职股东银行卡信息
     * @return
     */
    @RequestMapping("/bank/card")
    public BaseDto getBankCard(){
        BaseDto baseDto = new BaseDto();
        Long userId = MySysUser.id();
        UserBankCard userBankCard = userBankCardService.selectUserBankCard(2,Integer.valueOf(userId.toString()));
        if(null != userBankCard){
            baseDto.setData("1");
        }else{
            baseDto.setData("2");
        }
        return baseDto;
    }


    /**
     * 编辑兼职股东信息
     * @param userId
     * @param nickName
     * @param tel
     * @return
     */
    @RequestMapping(value = "/saveSysUser",method = RequestMethod.POST)
    public BaseDto saveSysUser(Integer userId,String nickName,String tel){
        return sysUserService.saveSysUser(userId,nickName,tel);
    }

    /**
     * 查询所有城市代理商列表
     * @return
     */
    @RequestMapping("/agent/list")
    public BaseDto agentList(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                        @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize,
                             Integer province,Integer city,Integer district) {
        Integer startPageNo = null;
        if (null != pageSize && null != pageNum) {
            startPageNo = (pageNum - 1) * pageSize;
        }
        BaseDto baseDto = new BaseDto();
        baseDto.setData(sysUserService.selectCityAgentList(2,startPageNo,pageSize,province,city,district));
        return baseDto;
    }

    /**
     * 新增城市代理商管理
     * 城市代理商昵称、手机号、所在区域、密码、佣金比例
     * @param
     * @return
     */
    @RequestMapping(value = "/agent/add",method = RequestMethod.POST)
    public BaseDto addAgent(String nickName,String tel,Integer province,Integer city,Integer district,String password,Double commRate){
        Long userId = MySysUser.id();
        return sysUserService.addAgent(userId,nickName,tel,province,city,district,password,commRate);
    }

    /**
     * 编辑城市代理商管理
     * 可编辑城市代理商昵称、手机号、所在区域、佣金比例（佣金比例修改以后新产生的商城订单手续费按照新佣金比例执行)
     * @param userId
     * @return
     */
    @RequestMapping(value = "/agent/update",method = RequestMethod.POST)
    public BaseDto updateAgent(Integer userId,String nickName,String tel,Integer province,Integer city,Integer district,Double commRate){
        return sysUserService.updateAgent(userId,nickName,tel,province,city,district,commRate);
    }

    /**
     * 城市代理商管理
     * 重置密码
     * @param userId
     * @return
     */
    @RequestMapping("/reset/password")
    public BaseDto resetPassword(Integer userId){
        return sysUserService.resetPassword(Long.valueOf(userId));
    }

    /**
     *
     * 城市代理商管理
     * 点击业绩订单可查看当前城市代理商下属的订单列表
     * @Param userId  城市代理商userid
     */
    @RequestMapping("/performanceOrders")
    public BaseDto performanceOrders(Integer pageNo, Integer pageSize,Long userId){
        Integer startPageNo = null;
        if (null != pageSize && null != pageNo) {
            startPageNo = (pageNo - 1) * pageSize;
        }
        return sysUserService.agentOrderList(startPageNo,pageSize,userId);
    }

    /**
     *
     * 城市代理商后台
     * 获取城市代理商业务员列表
     */
     @RequestMapping("/saleman/list")
     public BaseDto salemanList(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                                @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize){
         Long userId = MySysUser.id();
         Integer startPageNo = null;
         if (null != pageSize && null != pageNum) {
             startPageNo = (pageNum - 1) * pageSize;
         }
         return sysUserService.salemanList(startPageNo,pageSize,userId);
     }

    /**
     *
     * 城市代理商后台
     * 查看当前业务员下属店铺的订单数据
     */
     @RequestMapping("shop/order")
     public BaseDto shopOrder(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                              @RequestParam(defaultValue = "10",value = "pageSize")Integer pageSize,
                              Long userId){
         Integer startPageNo = null;
         if (null != pageSize && null != pageNum) {
             startPageNo = (pageNum - 1) * pageSize;
         }
         return sysUserService.shopOrder(startPageNo,pageSize,userId);
     }


    /**
     * 城市代理商后台
     * 新增业务员 业务员名称、手机号、佣金比例（业务员的佣金比例不能高于城市代理商自己的比例）、密码
     */
    @RequestMapping("/saleman/add")
    public BaseDto addSaleman(String nickName, String tel, BigDecimal commRate,String password){
        Long userId = MySysUser.id();
        return sysUserService.addSaleman(nickName,tel,commRate,password,userId);
    }

    /**
     * 城市代理商后台
     * 编辑业务员
     * @param userId
     * @param nickName
     * @param tel
     * @param commRate
     * @return
     */
    @RequestMapping("saleman/update")
    public BaseDto updateSaleman(Long userId,String nickName,String tel,Double commRate){
        return sysUserService.updateSaleman(userId,nickName,tel,commRate);
    }


    @RequestMapping("city/agent/detail")
    public BaseDto cityAgentDetail(@RequestParam(defaultValue = "1",value = "pageNum")Integer pageNum,
                                   @RequestParam(defaultValue = "1",value = "pageSize")Integer pageSize){
        Long userId = MySysUser.id();
        BaseDto baseDto = sysUserService.cityAgentDetail(userId);
        return baseDto;
    }

    /**
     * 获取当前城市代理商的佣金比例
     * @return
     */
    @RequestMapping("/get/commrate")
    public BaseDto getAgentCommRate(){
        Long userId = MySysUser.id();
        return sysUserService.getAgentDetail(userId);
    }

    /**
     * 获取业务员未结算金额
     */
    @RequestMapping("/get/unsettledComm")
    public BaseDto getUnsettledComm(Long userId){
        return cityAgentCommissionService.selectByUserId(userId);
    }

    /**
     * 结算业务员佣金
     * @return
     */
    @RequestMapping("/settle/saleman/comm")
    public BaseDto settleSalemanComm(Long userId,BigDecimal needMoney){
        return cityAgentCommissionService.settleSalemanComm(userId,needMoney);
    }





}
